<template>
	<view>
		<!-- 重绘导航栏 -->
		<u-navbar :is-back="true" title="擅长项目" title-color="white" :background="background" :border-bottom="false">
			<view class="navbar-right" slot="right" style="padding-right: 20rpx;color: #FFFFFF;" @click="confirm()">
				确定
			</view>
		</u-navbar>
		<view class="u-p-l-26 u-p-r-26">
			<view class="" v-for="(item,index) in projectList" :key="index" @click="swithCheck(index)"
				style="line-height: 70rpx;border-bottom: #f0f0f0 1px solid;">
				{{item.collection_name}}
				<image src="../../../../static/index/zzrz/check-icon.png" v-show="item.selected==true"
					style="width: 20rpx;height: 20rpx;float: right;margin-top: 25rpx;" mode=""></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: {
					backgroundColor: '#00a8ff',
				},
				projectList: [],
				ids: []
			}
		},
		onLoad() {
			// 获取医生擅长项目
			this.$apiBasics.collections().then((res) => {
				console.log('request success', res);
				// 分组列表
				this.projectList = res.data.data;
			}).catch((err) => {
				console.log('request fail', err);
			});
		},
		methods: {
			swithCheck(index) {

				console.log("点击了：" + index)

				let x = 1;
				for (let i = 0; i < this.projectList.length; i++) {
					if (this.projectList[i].selected == true) {
						x += 1;
						this.ids.push(this.projectList[i].id)
					}
				}
				if (x > 3) {
					console.log("大于3个");
					if (this.projectList[index].selected = true) {
						this.projectList[index].selected = !this.projectList[index].selected;
					}
				} else if (x < 4) {
					this.projectList[index].selected = !this.projectList[index].selected;
				}
			},
			// 确定事件
			confirm(e) {
				let newArr = new Set(this.ids);
				console.log(newArr);
				console.log(newArr instanceof Array);
				newArr = Array.from(newArr);
				console.log(newArr instanceof Array);
				this.ids = newArr;
				uni.setStorageSync('ids', this.ids);
				uni.navigateBack();
			}
		}
	}
</script>

<style scoped lang="scss">

</style>
